<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Safeips;
use App\Models\Blackdomains;
use Auth;

class SafeController extends Controller
{
    public function safeIps(Request $request)
    {
        $users = Auth::user();
        if ($users->authority != 3) return redirect("/admin/links_lists/lists")->with("status","error")->with("msg","您无权访问本链接！");
        $safeips = Safeips::getSafeIps($request->links_id,$request->users_lists);
        return view('admin.safe.safe_ips',["safeips"=>$safeips,"condition"=>$request->all()]);
    }

    public function blackList(Request $request)
    {
        $users = Auth::user();
        if ($users->authority != 3) return redirect("/admin/links_lists/lists")->with("status","error")->with("msg","您无权访问！");
        $blacklists = Blackdomains::getBlackLists($request->name,$request->domain);
        return view('admin.black.black_lists',["blacklists"=>$blacklists,"condition"=>$request->all()]);
    }

    public function blackAddSubmit(Request $request)
    {
        $users = Auth::user();
        $text = $request->id == 0 ? '添加':'更新';
        if ($users->authority != 3) return response()->json(["status"=>"error","code"=>"1001","msg"=>"您无权".$text."！"]);
        if($request->id == 0){
            Blackdomains::addBlack($request->name,$request->domain);
        }else{
            Blackdomains::updateBlack($request->id,$request->name,$request->domain);
        }
        return response()->json(["status"=>"success","code"=>"1000","msg"=>"黑名单".$text."成功！"]);
    }

    public function delete(Request $request)
    {
        $users = Auth::user();
        if ($users->authority != 3) return redirect("/admin/links_lists/lists")->with("status","error")->with("msg","您无权删除！");
        if(empty($request->id)) return redirect("/admin/links_lists/lists")->with("status","error")->with("msg","参数错误！");
        Blackdomains::deleteBlack($request->id);
        return redirect("/admin/black/blacklist")->with("status","success")->with("msg","黑名单删除成功");
    }

}
